//
//  YWNoReadReplyTableViewController.m
//  maintain
//
//  Created by Kyo on 16/4/20.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWNoReadReplyTableViewController.h"
#import "YWMaterialApplyDetailTableViewController.h"
#import "YWMaterialStockOutDetailTableViewController.h"
#import "YWMaterialReturnBackDetailTableViewController.h"
#import "YWAssetsInventoryDetailsTableViewController.h"
#import "YWDetailNoticeViewController.h"
#import "YWDetailWorkNoticeViewController.h"

#import "UIColor+CustomColors.h"
#import "YWWorkNoticeMessageCell.h"
#import "YWReply.h"
#import "YWMaterialManager.h"
#import "YWNotice.h"
#import "YWTempMission.h"
#import "YWNoticeManager.h"
#import "YWTempMissionManager.h"
#import "YWTempMission.h"
#import "YWAssetCheckRecord.h"
#import "YWAssetCheckManager.h"
#import "YWMissionManager.h"
#import "YWMission.h"
#import "YWTroubleReplyTableViewController.h"
#import "YWDetectAndUpkeepReplyTableViewController.h"
#import "YWInspectionReplyTableViewController.h"
#import "YWInspectionMissionManager.h"
#import "YWKeepWatchMissionManager.h"
#import "YWKeepWatchReplyTableViewController.h"
#import "YWDetectAndUpkeepManager.h"
#import "YWTroubleMissionModel.h"
#import "YWTroubleMissionManager.h"
#import "YWReportTroubleModel.h"
#import "YWReportTroubleManager.h"

@interface YWNoReadReplyTableViewController ()

@end

@implementation YWNoReadReplyTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = NSLocalizedString(@"messsge", nil);//@"消息";
    self.view.backgroundColor = [UIColor customBackgroudColor];
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.tableView reloadData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    CGFloat rowH = [YWWorkNoticeMessageCell cellHeightOfReply:self.replysArray[indexPath.row]];
    return rowH;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.replysArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YWWorkNoticeMessageCell *cell = [YWWorkNoticeMessageCell cellWithTableView:tableView];
    YWReply *reply = self.replysArray[indexPath.row];
    cell.reply = reply;
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YWReply *reply = self.replysArray[indexPath.row];
    if (reply.relationType == YWReplyRelationTypeNotice) {
        YWNotice *notice = [[YWNoticeManager shareManager]noticeWithNoticeId:reply.relationId];
        if (notice) {
            YWDetailNoticeViewController *noticeDetailVC = [YWDetailNoticeViewController new];
            noticeDetailVC.notice = notice;
            [self.navigationController pushViewController:noticeDetailVC animated:YES];
        }
        
    }else if (reply.relationType == YWReplyRelationTypeTempMission){
        YWTempMission *tempMission  = [[YWTempMissionManager shareManager]tempMissionWithTempMissionId:reply.relationId];
        if (tempMission) {
            YWDetailWorkNoticeViewController *tempMissionDetailVC = [YWDetailWorkNoticeViewController new];
            tempMissionDetailVC.tempMission = tempMission;
            [self.navigationController pushViewController:tempMissionDetailVC animated:YES];
        }
    }else if (reply.relationType == YWReplyRelationTypeMaterialApply){
        YWMaterialApplyViewModel *viewModel = [[YWMaterialManager shareManager] materialApplyWithMaterialApplyId:reply.relationId];
        if (viewModel) {
            YWMaterialApplyDetailTableViewController * applyDetailVC = [[YWMaterialApplyDetailTableViewController alloc] init];
            applyDetailVC.needPopBack = YES;
            applyDetailVC.apply = viewModel;
            [self.navigationController pushViewController:applyDetailVC animated:YES];
        }
    }else if (reply.relationType == YWReplyRelationTypeMaterialReceive){
        YWMaterialStockOut *receipent = [[YWMaterialManager shareManager] materialReceipientWithMaterialReceipientId:reply.relationId];
        if (receipent) {
            YWMaterialStockOutDetailTableViewController * stockOutDetail = [[YWMaterialStockOutDetailTableViewController alloc] init];
            stockOutDetail.needPopBack = YES;
            stockOutDetail.receipient = receipent;
            [self.navigationController pushViewController:stockOutDetail animated:YES];
        }
    }else if (reply.relationType == YWReplyRelationTypeMaterialReturn){
        YWMaterialReturn *materialReturn = [[YWMaterialManager shareManager] materialReturnWithMaterialReturnId:reply.relationId];
        if (materialReturn) {
            YWMaterialReturnBackDetailTableViewController * returnBackDetail = [[YWMaterialReturnBackDetailTableViewController alloc]init];
            returnBackDetail.needPopBack = YES;
            returnBackDetail.returnBack = materialReturn;
            [self.navigationController pushViewController:returnBackDetail animated:YES];
        }
        
    }else if (reply.relationType == YWReplyRelationTypeAssetCheckRecord){
        YWAssetCheckRecord * checkRecord = [[YWAssetCheckManager shareManager] assetCheckRecordWithCheckRecordId:reply.relationId];
        if (checkRecord) {
            YWAssetsInventoryDetailsTableViewController * checkRecordDetail = [[YWAssetsInventoryDetailsTableViewController alloc] init];
            checkRecordDetail.needPopBack = YES;
            checkRecordDetail.checkRecord = checkRecord;
            [self.navigationController pushViewController:checkRecordDetail animated:YES];
        }
    }else if (reply.relationType == YWReplyRelationTypeTrouble){
        YWTroubleMissionModel * troubleMissionModel = [[YWTroubleMissionManager shareManager] troubleMissionModelWithTroubleId:reply.relationId];
        if (troubleMissionModel == nil) {
            YWReportTroubleModel *reportTrouble = [[YWReportTroubleManager shareManager] getOneReportTroubleWithTroubleId:reply.relationId];
            troubleMissionModel = reportTrouble.troubleMission;
        }
        if (troubleMissionModel) {
            YWTroubleReplyTableViewController *troubleReplyVC = [[YWTroubleReplyTableViewController alloc] init];
            troubleReplyVC.troubleMissionModel = troubleMissionModel;
            [self.navigationController pushViewController:troubleReplyVC animated:YES];
        }
    }else if (reply.relationType == YWReplyRelationTypeDetectAndUpkeep){
        YWMaintenanceMission *mission = [YWDetectAndUpkeepManager maintenanceMissionWithMissionId:reply.relationId];
        if (mission) {
            YWDetectAndUpkeepReplyTableViewController *detectAndUpkeepReplyVC = [[YWDetectAndUpkeepReplyTableViewController alloc] init];
            detectAndUpkeepReplyVC.mission = mission;
            [self.navigationController pushViewController:detectAndUpkeepReplyVC animated:YES];
        }
    }else if (reply.relationType == YWReplyRelationTypeInspection){
        YWInspectionMissionModel *inspectionMission = [[YWInspectionMissionManager shareManager] inspectionMissionWithMissionId:reply.relationId];
        if (inspectionMission) {
            YWInspectionReplyTableViewController *inspectionReplyVC = [[YWInspectionReplyTableViewController alloc] init];
            inspectionReplyVC.inspectionMission = inspectionMission;
            [self.navigationController pushViewController:inspectionReplyVC animated:YES];
        }
        
    }else if (reply.relationType == YWReplyRelationTypeKeepWatch){
        YWKeepWatchMission *keepWatchMission = [[YWKeepWatchMissionManager shareManager] keepWatchMissionWithMissionId:reply.relationId];
        if (keepWatchMission) {
            YWKeepWatchReplyTableViewController *keepWatchReplyVC = [[YWKeepWatchReplyTableViewController alloc] init];
            keepWatchReplyVC.keepWatchMission = keepWatchMission;
            [self.navigationController pushViewController:keepWatchReplyVC animated:YES];
        }
    }
    
}
@end
